package com.web.base.wechat.dao;

import com.blue.jdbc.BaseDao;
import com.web.base.wechat.model.WechatMenu;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 微信菜单数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月27日
 */
@Repository("wechatMenuDao")
public class WechatMenuDao extends BaseDao<WechatMenu>
{
	public WechatMenuDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, WechatMenu param)
	{
		if (param != null)
		{
			if (param.getParentId() != null && param.getParentId() > 0)
				sql.append(" and a.parent_id=:parentId");
				
			if (param.getWechatId() != null && param.getWechatId() > 0)
				sql.append(" and a.wechat_id=:wechatId");
				
			if (param.getName() != null && !param.getName().isEmpty())
			{
				sql.append(" and a.name like :name");
				param.setName("%" + param.getName() + "%");
			}
			
			if (param.getState() != null)
				sql.append(" and a.state=:state");
				
		}
		sql.append(" order by a.sort");
	}

	@Override
	protected StringBuilder select()
	{
		StringBuilder sql = new StringBuilder(64);
		sql.append("select a.*, aa.name parent_name, w.name wechat_name from wx_wechat_menu a");
		sql.append(" left join wx_wechat_menu aa on aa.id=a.parent_id");
		sql.append(" left join wx_wechat w on w.id=a.wechat_id");
		sql.append(" where 1=1");
		return sql;
	}

	public List<WechatMenu> listRoot()
	{
		String sql = "select * from wx_wechat_menu where state=0 and parent_id is null";
		return jdbcObjectTemplate.list(WechatMenu.class, sql);
	}
	
}
